package com.xindaoapp.happypet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;

/**
 * Web 付款
 * 
 * @author
 * 
 */
public class WebPayActivity extends UMBaseActivity implements OnClickListener {
    private String orderName;
    private String orderId;
    private Float orderPrice;
    private ImageButton back;
    private WebView wv;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wabpay);

        orderName = getIntent().getStringExtra("orderName");
        orderId = getIntent().getStringExtra("orderId");
        orderPrice = getIntent().getFloatExtra("orderPrice", -1);
        // http://silutrip.com/alipay/alipayapi.php?
        // WIDout_trade_no=2014053028560&WIDsubject=狗狗饼干&WIDtotal_fee=0.01&WIDis_buy=1&uuid=1281019

        url =
                Address.ALIPAY + "?WIDout_trade_no=" + orderId + "&WIDsubject=" + orderName
                        + "&WIDtotal_fee=" + orderPrice + "&uuid=" + UserState.getUserUid();
        // url=Address.ALIPAY;
        Log.e("111", url);

        initView();
        initData();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.cancel);
        wv = (WebView) findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        back.setOnClickListener(this);
    }

    private void initData() {
        // wv.requestFocus();
        // wv.performClick();
        wv.loadUrl(url);
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            // 页面载入完成后调用
            @Override
            public void onPageFinished(WebView view, String url) {
                if (url.toString().contains("result=success")) {
                    WebPayActivity.this.finish();
                    Intent my_order = new Intent(WebPayActivity.this, MyOrderListActivity.class);
                    startActivity(my_order);
                }
                super.onPageFinished(view, url);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            default:
                break;
        }
    }

}
